/**
 *
 */
package cn.jhz.learn.blog.config;

import cn.jhz.learn.blog.config.executor.AsyncConfiguration;
import cn.jhz.learn.blog.config.security.PermissionPreProcessor;
import cn.jhz.learn.blog.config.security.SecurityConfig;
import cn.jhz.learn.blog.service.security.permission.PermissionService;

import org.springframework.context.annotation.*;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.io.IOException;


/**
 * @author asus
 *
 */

@Configuration
@Import({SecurityConfig.class, AsyncConfiguration.class})
@EnableWebMvc
@ComponentScan(
        basePackages =
                {
                        "cn.jhz.learn.blog.controller",
                        "cn.jhz.learn.blog.service",
                        "cn.jhz.learn.blog.common.util"
                },
        includeFilters =
                {
                        @Filter(
                            type = FilterType.ANNOTATION,
                            value = {
                                    Service.class,
                                    RestController.class,
                                    RestControllerAdvice.class,
                                    Component.class
                            })
                })
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.enableContentNegotiation(new MappingJackson2JsonView());
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/api/**")
                .allowedOrigins("http://localhost:8080")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true);
    }

    @Bean
    public PermissionPreProcessor permissionPreProcessor(PermissionService permissionService
            , RequestMappingHandlerMapping requestMappingHandlerMapping){

        return new PermissionPreProcessor(permissionService, requestMappingHandlerMapping);
    }

    @Bean
    public MultipartResolver multipartResolver() throws IOException {
        return new StandardServletMultipartResolver();
    }
}
